package com.shsh.safemanage.action;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.shsh.safemanage.dao.AccidentDao;
import com.shsh.safemanage.dao.DaoCreator;
import com.shsh.safemanage.dao.ProblemDao;
import com.shsh.safemanage.model.AccidentFacade;
import com.shsh.safemanage.model.Defect;
import com.shsh.safemanage.model.Other;
import com.shsh.safemanage.model.Problem;
import com.shsh.safemanage.model.ProblemBrowseCondition;
import com.shsh.safemanage.model.Shutdown;
import com.shsh.safemanage.model.Tendency;
import com.shsh.safemanage.model.User;
import com.shsh.safemanage.util.Constants;
import com.shsh.safemanage.util.RequestUtil;
import com.shsh.safemanage.util.Util;

public class BrowseProblemAction {
	public List<Problem> browseProblems(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null, writerManageGroupIDStr = null, writerDepartment1IDStr = null, writerDepartment2IDStr = null, pageStr = null, problemTypeStr = null, listTypeStr = null, roleLevelStr = null;

		pageStr = request.getParameter(Constants.REQUEST_PARAMETER_PAGE_NUM);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_PROBLEM_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
			writerManageGroupIDStr = condition.getWriterManageGroupID();
			writerDepartment1IDStr = condition.getWriterDepartment1ID();
			writerDepartment2IDStr = condition.getWriterDepartment2ID();
			listTypeStr = condition.getListTypeStr();
			problemTypeStr = condition.getProblemTypeStr();
			roleLevelStr = condition.getRoleLevelStr();
			if (pageStr == null) {
				pageStr = condition.getPageStr();
			} else {
				condition.setPageStr(pageStr);
			}
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);
			writerManageGroupIDStr = request.getParameter(Constants.REQUEST_PARAMETER_MANAGEGROUP_ID);
			writerDepartment1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID_1);
			writerDepartment2IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2ID);
			problemTypeStr = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TYPE);
			listTypeStr = request.getParameter(Constants.REQUEST_PARAMETER_LIST_TYPE);
			roleLevelStr = request.getParameter(Constants.REQUEST_PARAMETER_ROLE_LEVEL);

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);
			condition.setWriterManageGroupID(writerManageGroupIDStr);
			condition.setWriterDepartment1ID(writerDepartment1IDStr);
			condition.setWriterDepartment2ID(writerDepartment2IDStr);
			condition.setProblemTypeStr(problemTypeStr);
			condition.setListTypeStr(listTypeStr);
			condition.setPageStr(pageStr);
			condition.setRoleLevelStr(roleLevelStr);

			session.setAttribute(Constants.SESSION_PROBLEM_CONDITION, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long writerManageGroupID = null;
		try {
			writerManageGroupID = new Long(writerManageGroupIDStr);
		} catch (Exception e) {
		}

		Long writerDepartment1ID = null;
		try {
			writerDepartment1ID = new Long(writerDepartment1IDStr);
		} catch (Exception e) {
		}

		Long writerDepartment2ID = null;
		try {
			writerDepartment2ID = new Long(writerDepartment2IDStr);
		} catch (Exception e) {
		}

		int page = 1;
		try {
			page = Integer.parseInt(pageStr);
		} catch (Exception e) {
		}

		Integer problemType = null;
		try {
			problemType = new Integer(problemTypeStr);
		} catch (Exception e) {
		}

		if (depart1ID == null && equipID == null && Util.isNullOrEmpty(problemTitle) && Util.isNullOrEmpty(problemContent)
				&& Util.isNullOrEmpty(dateStart) && Util.isNullOrEmpty(dateEnd)) {
			Calendar now = Calendar.getInstance();
			dateEnd = Util.formatDate(now.getTime());
			now.add(Calendar.DATE, -30);
			dateStart = Util.formatDate(now.getTime());
		}

		int roleLevel = 0;
		try {
			roleLevel = Integer.parseInt(roleLevelStr);
		} catch (Exception e) {
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();
		Map<String, Object> resultMap = problemDao.searchProblems(depart1ID, equipID, writerManageGroupID, problemTitle, problemContent,
				dateStart, dateEnd, null, writerDepartment1ID, writerDepartment2ID, problemType, roleLevel, page, Constants.PAGE_SIZE);
		List<Problem> problems = (List<Problem>) resultMap.get(Constants.DAO_RESULT);
		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));

		return problems;
	}

	public List<Problem> browseDuchayuanProblems(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null, writerManageGroupIDStr = null, writerDepartment1IDStr = null, writerDepartment2IDStr = null, pageStr = null, problemTypeStr = null, listTypeStr = null;

		pageStr = request.getParameter(Constants.REQUEST_PARAMETER_PAGE_NUM);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_PROBLEM_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
			writerManageGroupIDStr = condition.getWriterManageGroupID();
			writerDepartment1IDStr = condition.getWriterDepartment1ID();
			writerDepartment2IDStr = condition.getWriterDepartment2ID();
			listTypeStr = condition.getListTypeStr();
			problemTypeStr = condition.getProblemTypeStr();
			if (pageStr == null) {
				pageStr = condition.getPageStr();
			} else {
				condition.setPageStr(pageStr);
			}
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);
			writerManageGroupIDStr = request.getParameter(Constants.REQUEST_PARAMETER_MANAGEGROUP_ID);
			writerDepartment1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID_1);
			writerDepartment2IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2ID);
			problemTypeStr = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TYPE);
			listTypeStr = request.getParameter(Constants.REQUEST_PARAMETER_LIST_TYPE);

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);
			condition.setWriterManageGroupID(writerManageGroupIDStr);
			condition.setWriterDepartment1ID(writerDepartment1IDStr);
			condition.setWriterDepartment2ID(writerDepartment2IDStr);
			condition.setProblemTypeStr(problemTypeStr);
			condition.setListTypeStr(listTypeStr);
			condition.setPageStr(pageStr);

			session.setAttribute(Constants.SESSION_PROBLEM_CONDITION, condition);
		}

		int page = 1;
		try {
			page = Integer.parseInt(pageStr);
		} catch (Exception e) {
		}

		if (Util.isNullOrEmpty(dateStart) && Util.isNullOrEmpty(dateEnd)) {
			Calendar now = Calendar.getInstance();
			dateEnd = Util.formatDate(now.getTime());
			now.add(Calendar.DATE, -60);
			dateStart = Util.formatDate(now.getTime());
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();
		Map<String, Object> resultMap = problemDao.searchDuchayuanProblems(dateStart, dateEnd, Constants.ROLE_LEVEL_KEJI, page,
				Constants.PAGE_SIZE);
		List<Problem> problems = (List<Problem>) resultMap.get(Constants.DAO_RESULT);
		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));

		return problems;
	}

	public List<Problem> browsePostchecks(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String postcheckIDStr = null, depart1IDStr = null, equipIDStr = null, postcheckContentIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null;

		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_POSTCHECK_CONDITION);
		if (condition != null) {
			postcheckIDStr = condition.getPostcheckIDStr();
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			postcheckContentIDStr = condition.getPostcheckContentIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
		} else {
			postcheckIDStr = request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_ID);
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			postcheckContentIDStr = request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_CONTENT_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (postcheckContentIDStr == null) {
				postcheckContentIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			condition = new ProblemBrowseCondition();
			condition.setPostcheckIDStr(postcheckIDStr);
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setPostcheckContentIDStr(postcheckContentIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);

			session.setAttribute(Constants.SESSION_POSTCHECK_CONDITION, condition);
		}

		Long postcheckID = null;
		try {
			postcheckID = new Long(postcheckIDStr);
		} catch (Exception e) {
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long postcheckContentID = null;
		try {
			postcheckContentID = Long.parseLong(postcheckContentIDStr);
		} catch (Exception e) {
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();

		if (depart1ID == null || depart1ID == 0) {
			Map<String, Object> resultMap = problemDao.searchPostchecks(postcheckID, equipID, postcheckContentID, problemTitle,
					problemContent, dateStart, dateEnd, pageNum);
			request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
			return (List<Problem>) resultMap.get(Constants.DAO_RESULT);
		}

		Map<String, Object> resultMap = problemDao.searchPostchecks(postcheckID, depart1ID, equipID, postcheckContentID, problemTitle,
				problemContent, dateStart, dateEnd, depart1ID, pageNum);

		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Problem>) resultMap.get(Constants.DAO_RESULT);
	}

	public List<Problem> browseProblemsForDefect(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null, writerManageGroupIDStr = null;
		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);
		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_PROBLEM_DEFECT_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
			writerManageGroupIDStr = condition.getWriterManageGroupID();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);
			writerManageGroupIDStr = request.getParameter(Constants.REQUEST_PARAMETER_MANAGEGROUP_ID);

			Boolean init = null;
			try {
				init = Boolean.parseBoolean(request.getParameter(Constants.REQUEST_PARAMETER_INIT));
			} catch (Exception e) {
			}

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			if (writerManageGroupIDStr == null) {
				writerManageGroupIDStr = "";
			}

			if (init != null && init == Boolean.TRUE) {
				User user = (User) session.getAttribute(Constants.SESSION_USER);
				depart1IDStr = String.valueOf(user.getDepartment1ID());
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);
			condition.setWriterManageGroupID(writerManageGroupIDStr);

			session.setAttribute(Constants.SESSION_PROBLEM_DEFECT_CONDITION, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long writerManageGroupID = null;
		try {
			writerManageGroupID = new Long(writerManageGroupIDStr);
		} catch (Exception e) {
		}

		if (depart1ID == null && equipID == null && Util.isNullOrEmpty(problemTitle) && Util.isNullOrEmpty(problemContent)
				&& Util.isNullOrEmpty(dateStart) && Util.isNullOrEmpty(dateEnd)) {
			Calendar now = Calendar.getInstance();
			dateEnd = Util.formatDate(now.getTime());
			now.add(Calendar.DATE, -30);
			dateStart = Util.formatDate(now.getTime());
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();
		Map<String, Object> resultMap = problemDao.searchNormalProblems(depart1ID, equipID, writerManageGroupID, problemTitle,
				problemContent, dateStart, dateEnd, null, null, null, pageNum);
		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Problem>) resultMap.get(Constants.DAO_RESULT);
	}

	public List<Problem> browseProblemsForShutdown(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null, writerManageGroupIDStr = null;
		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		Integer problemType = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PROBLEM_TYPE, (Integer) null);
		String sessionKey = Shutdown.getSessionKey(problemType);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(sessionKey);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
			writerManageGroupIDStr = condition.getWriterManageGroupID();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);
			writerManageGroupIDStr = request.getParameter(Constants.REQUEST_PARAMETER_MANAGEGROUP_ID);

			Boolean init = null;
			try {
				init = Boolean.parseBoolean(request.getParameter(Constants.REQUEST_PARAMETER_INIT));
			} catch (Exception e) {
			}

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			if (writerManageGroupIDStr == null) {
				writerManageGroupIDStr = "";
			}

			if (init != null && init == Boolean.TRUE) {
				User user = (User) session.getAttribute(Constants.SESSION_USER);
				depart1IDStr = String.valueOf(user.getDepartment1ID());
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);
			condition.setWriterManageGroupID(writerManageGroupIDStr);

			session.setAttribute(sessionKey, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long writerManageGroupID = null;
		try {
			writerManageGroupID = new Long(writerManageGroupIDStr);
		} catch (Exception e) {
		}

		if (depart1ID == null && equipID == null && Util.isNullOrEmpty(problemTitle) && Util.isNullOrEmpty(problemContent)
				&& Util.isNullOrEmpty(dateStart) && Util.isNullOrEmpty(dateEnd)) {
			Calendar now = Calendar.getInstance();
			dateEnd = Util.formatDate(now.getTime());
			now.add(Calendar.DATE, -30);
			dateStart = Util.formatDate(now.getTime());
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();
		Map<String, Object> resultMap = problemDao.searchNormalProblems(depart1ID, equipID, writerManageGroupID, problemTitle,
				problemContent, dateStart, dateEnd, null, null, null, pageNum);

		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Problem>) resultMap.get(Constants.DAO_RESULT);
	}

	public List<Problem> browseProblemsForTendency(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null, writerManageGroupIDStr = null;
		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_PROBLEM_TENDENCY_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
			writerManageGroupIDStr = condition.getWriterManageGroupID();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);
			writerManageGroupIDStr = request.getParameter(Constants.REQUEST_PARAMETER_MANAGEGROUP_ID);

			Boolean init = null;
			try {
				init = Boolean.parseBoolean(request.getParameter(Constants.REQUEST_PARAMETER_INIT));
			} catch (Exception e) {
			}

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			if (writerManageGroupIDStr == null) {
				writerManageGroupIDStr = "";
			}

			if (init != null && init == Boolean.TRUE) {
				User user = (User) session.getAttribute(Constants.SESSION_USER);
				depart1IDStr = String.valueOf(user.getDepartment1ID());
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);
			condition.setWriterManageGroupID(writerManageGroupIDStr);

			session.setAttribute(Constants.SESSION_PROBLEM_TENDENCY_CONDITION, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long writerManageGroupID = null;
		try {
			writerManageGroupID = new Long(writerManageGroupIDStr);
		} catch (Exception e) {
		}

		if (depart1ID == null && equipID == null && Util.isNullOrEmpty(problemTitle) && Util.isNullOrEmpty(problemContent)
				&& Util.isNullOrEmpty(dateStart) && Util.isNullOrEmpty(dateEnd)) {
			Calendar now = Calendar.getInstance();
			dateEnd = Util.formatDate(now.getTime());
			now.add(Calendar.DATE, -30);
			dateStart = Util.formatDate(now.getTime());
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();
		Map<String, Object> resultMap = problemDao.searchNormalProblems(depart1ID, equipID, writerManageGroupID, problemTitle,
				problemContent, dateStart, dateEnd, null, null, null, pageNum);

		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Problem>) resultMap.get(Constants.DAO_RESULT);
	}

	public List<Defect> browseDefects(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, defectTypeIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null;
		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_DEFECT_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			defectTypeIDStr = condition.getDefectTypeIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			defectTypeIDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEFECT_TYPE_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (defectTypeIDStr == null) {
				defectTypeIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setDefectTypeIDStr(defectTypeIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);

			session.setAttribute(Constants.SESSION_DEFECT_CONDITION, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long defectTypeID = null;
		try {
			defectTypeID = Long.parseLong(defectTypeIDStr);
		} catch (Exception e) {
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();

		Map<String, Object> resultMap = problemDao.searchDefects(depart1ID, equipID, defectTypeID, problemTitle, problemContent, dateStart,
				dateEnd, pageNum);

		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Defect>) resultMap.get(Constants.DAO_RESULT);
	}

	public List<Tendency> browseTendencys(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, tendencyTypeIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null;
		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_TENDENCY_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			tendencyTypeIDStr = condition.getTendencyTypeIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			tendencyTypeIDStr = request.getParameter(Constants.REQUEST_PARAMETER_TENDENCY_TYPE_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (tendencyTypeIDStr == null) {
				tendencyTypeIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setTendencyTypeIDStr(tendencyTypeIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);

			session.setAttribute(Constants.SESSION_TENDENCY_CONDITION, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long tendencyTypeID = null;
		try {
			tendencyTypeID = Long.parseLong(tendencyTypeIDStr);
		} catch (Exception e) {
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();

		Map<String, Object> resultMap = problemDao.searchTendencys(depart1ID, equipID, tendencyTypeID, problemTitle, problemContent,
				dateStart, dateEnd, pageNum);

		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Tendency>) resultMap.get(Constants.DAO_RESULT);
	}

	public List<Shutdown> browseShutdowns(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, shutdownTypeIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null, escalateZongbuStr = null;
		String problemTypeStr = null;
		Integer problemType = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PROBLEM_TYPE, (Integer) null);
		String sessionKey = Shutdown.getBrowseSessionKey(problemType);
		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(sessionKey);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			shutdownTypeIDStr = condition.getShutdownTypeIDStr();
			escalateZongbuStr = condition.getEscalateZongbuStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
			problemTypeStr = condition.getProblemTypeStr();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			shutdownTypeIDStr = request.getParameter(Constants.REQUEST_PARAMETER_SHUTDOWN_TYPE_ID);
			Boolean escalateZongbuParam = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_ESCALATE_ZONGBU, (Boolean) null);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);
			problemTypeStr = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TYPE);

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (shutdownTypeIDStr == null) {
				shutdownTypeIDStr = "";
			}

			if (escalateZongbuParam == null) {
				escalateZongbuStr = "";
			} else {
				escalateZongbuStr = String.valueOf(escalateZongbuParam);
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setShutdownTypeIDStr(shutdownTypeIDStr);
			condition.setEscalateZongbuStr(escalateZongbuStr.trim());
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);
			condition.setProblemTypeStr(problemTypeStr);

			session.setAttribute(sessionKey, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long shutdownTypeID = null;
		try {
			shutdownTypeID = Long.parseLong(shutdownTypeIDStr);
		} catch (Exception e) {
		}

		Boolean escalateZongbu = null;
		try {
			if (escalateZongbuStr.equalsIgnoreCase("true") || escalateZongbuStr.equalsIgnoreCase("false")) {
				escalateZongbu = new Boolean(escalateZongbuStr);
			}
		} catch (Exception e) {
			escalateZongbu = null;
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();

		Map<String, Object> resultMap = problemDao.searchShutdowns(problemType, depart1ID, equipID, shutdownTypeID, escalateZongbu,
				problemTitle, problemContent, dateStart, dateEnd, pageNum);

		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Shutdown>) resultMap.get(Constants.DAO_RESULT);
	}

	public List<AccidentFacade> browseAccidents(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, accidentTypeIDStr = null, problemTitle = null, dateStart = null, dateEnd = null;

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_ACCIDENT_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			accidentTypeIDStr = condition.getAccidentTypeIDStr();
			problemTitle = condition.getProblemTitle();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			accidentTypeIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ACCIDENT_TYPE_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (accidentTypeIDStr == null) {
				accidentTypeIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setAccidentTypeIDStr(accidentTypeIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);

			session.setAttribute(Constants.SESSION_ACCIDENT_CONDITION, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long tendencyTypeID = null;
		try {
			tendencyTypeID = Long.parseLong(accidentTypeIDStr);
		} catch (Exception e) {
		}

		AccidentDao accidentDao = DaoCreator.createAccidentDao();

		List<AccidentFacade> accidents = accidentDao.searchAccident(null, depart1ID, equipID, tendencyTypeID, problemTitle, dateStart,
				dateEnd, null, Constants.ACCIDENT_STATUS_COMPLETE);

		return accidents;
	}

	public List<Other> browseOthers(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String depart1IDStr = null, equipIDStr = null, defectTypeIDStr = null, problemTitle = null, problemContent = null, dateStart = null, dateEnd = null;
		int pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		ProblemBrowseCondition condition = (ProblemBrowseCondition) session.getAttribute(Constants.SESSION_OTHER_CONDITION);
		if (condition != null) {
			depart1IDStr = condition.getDepart1IDStr();
			equipIDStr = condition.getEquipIDStr();
			defectTypeIDStr = condition.getDefectTypeIDStr();
			problemTitle = condition.getProblemTitle();
			problemContent = condition.getProblemContent();
			dateStart = condition.getDateStart();
			dateEnd = condition.getDateEnd();
		} else {
			depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
			equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
			defectTypeIDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEFECT_TYPE_ID);
			problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
			problemTitle = Util.isoToUTF8(problemTitle);
			problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
			problemContent = Util.isoToUTF8(problemContent);
			dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
			dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

			if (depart1IDStr == null) {
				depart1IDStr = "";
			}

			if (equipIDStr == null) {
				equipIDStr = "";
			}

			if (defectTypeIDStr == null) {
				defectTypeIDStr = "";
			}

			if (problemTitle == null) {
				problemTitle = "";
			}

			if (problemContent == null) {
				problemContent = "";
			}

			if (dateStart == null) {
				dateStart = "";
			}

			if (dateEnd == null) {
				dateEnd = "";
			}

			condition = new ProblemBrowseCondition();
			condition.setDepart1IDStr(depart1IDStr);
			condition.setEquipIDStr(equipIDStr);
			condition.setDefectTypeIDStr(defectTypeIDStr);
			condition.setProblemTitle(problemTitle);
			condition.setProblemContent(problemContent);
			condition.setDateStart(dateStart);
			condition.setDateEnd(dateEnd);

			session.setAttribute(Constants.SESSION_OTHER_CONDITION, condition);
		}

		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}

		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}

		Long defectTypeID = null;
		try {
			defectTypeID = Long.parseLong(defectTypeIDStr);
		} catch (Exception e) {
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();

		Map<String, Object> resultMap = problemDao.searchOthers(depart1ID, equipID, defectTypeID, problemTitle, problemContent, dateStart,
				dateEnd, pageNum);

		request.setAttribute(Constants.DAO_RESULT_NUMBER, resultMap.get(Constants.DAO_RESULT_NUMBER));
		return (List<Other>) resultMap.get(Constants.DAO_RESULT);
	}
}
